//
//  QrcodeViewController.m
//  SinaWeibo
//
//  Created by Apple on 16/8/8.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "MyQrcodeViewController.h"
#import "QrcodeViewController.h"
#import "AVFoundation/AVFoundation.h"


@interface QrcodeViewController ()<AVCaptureMetadataOutputObjectsDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *qrcodeConsY;

@property (nonatomic,strong) AVCaptureSession *captureSession;

@property (nonatomic,strong) AVCaptureVideoPreviewLayer  *previewLayer;

@property (nonatomic,strong) CADisplayLink *link;

//点击关闭按钮
- (IBAction)clickClose:(id)sender;

//点击生成二维码
- (IBAction)creatQRCode:(id)sender;

@end

@implementation QrcodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(slide)];
    
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    
    self.link = link;
    
    [self setAVSession];
    
    
}

#pragma mark ---- 设置输入输出会话
-(void)setAVSession{
    //获取输入设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    //根据输入设备创建输入对象
    AVCaptureDeviceInput *deviceInput = [[AVCaptureDeviceInput alloc]initWithDevice:device error:NULL];
    //创建输出对象
    AVCaptureMetadataOutput *dataOutput = [[AVCaptureMetadataOutput alloc]init];
    
    //设置输出对象的代理
    [dataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    //创建会话，将输入输出添加到会话中
    AVCaptureSession *captureSession = [[AVCaptureSession alloc]init];
    self.captureSession = captureSession;
    //不能重复加入，要先判断能否加入
    if ([captureSession canAddInput:deviceInput]) {
        [captureSession addInput:deviceInput];
    }
    if ([captureSession canAddOutput:dataOutput]) {
        [captureSession addOutput:dataOutput];
    }
    
    //设置输出的数据类型
    [dataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    
    //创建预览界面
    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
    
    self.previewLayer = previewLayer;
    
    previewLayer.frame = self.view.bounds;
    
    [self.view.layer insertSublayer:previewLayer atIndex:0];
    
    
    //开始采集数据
    [captureSession startRunning];
}

-(void)slide{
    
    self.qrcodeConsY.constant -= 5;
    if (self.qrcodeConsY.constant == -170) {
        self.qrcodeConsY.constant = 170;
    }
    
}

#pragma mark -----点击关闭按钮
- (IBAction)clickClose:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"关闭扫描");
    }];
    
}



#pragma mark ----- AVCaptureMetadataOutputObjectsDelegate
//只要解析道数据就会掉用
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    
    //判断是否扫面倒数据
    if (metadataObjects > 0 ) {
        
        //有数据后停止会话
        [self.captureSession stopRunning];
        
        //移除预览界面
        [self.previewLayer removeFromSuperlayer];
        
        
        //取出数据
        AVMetadataMachineReadableCodeObject *obj = metadataObjects.lastObject;
        
        
        //停止动画
        [self.link invalidate];
        self.link = nil;
        
        //显示数据
        UILabel *label = [[UILabel alloc]initWithFrame:self.view.bounds];
        
        label.numberOfLines = 0;
        
        label.text = obj.stringValue;
        
        [self.view addSubview:label];
        
        
    }
    
    NSLog(@"scanscan------");
    
}

#pragma mark -----跳转到生成二维码的控制器
- (IBAction)creatQRCode:(id)sender {
    
    //创建要跳转的控制器
    MyQrcodeViewController *myQrcode = [[MyQrcodeViewController alloc ]init];
    
    [self.navigationController pushViewController:myQrcode animated:YES];
        
    
    
}
@end
